package com.boyue.dao.impl;

import com.boyue.dao.CommonDao;
import com.boyue.dao.SysUserDao;
import com.boyue.entity.SysUser;
import com.boyue.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

/**
 * @auth wzy
 * @date 2023/9/6 15:27
 */
public class SysUserDaoImpl extends CommonDao implements SysUserDao {
    @Override
    public SysUser findByUsername(String username) throws SQLException {
        String sql = "SELECT * from sys_user where userName=?";
        List<SysUser> sysUsers = super.executeQuery(SysUser.class, sql, new Object[]{username});
        if (sysUsers != null && sysUsers.size()>0){
            return sysUsers.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<SysUser> listPage(PageInfo pageInfo) throws SQLException {
        String sql = "SELECT * FROM `sys_user`";

        String sqlCondition = " where 1=1 ";
//        if (addressList.getAddName()!=null && addressList.getAddName().length()>0){
//            sqlCondition += " and addName like '%"+addressList.getAddName()+"%' ";
//        }
//
//        if (addressList.getAddPhone()!=null && addressList.getAddPhone().length()>0){
//            sqlCondition += " and addPhone like '%"+addressList.getAddPhone()+"%' ";
//        }
//
//        if (addressList.getAddRelation()!=null && addressList.getAddRelation().length()>0){
//            sqlCondition += " and addRelation like '%"+addressList.getAddRelation()+"%' ";
//        }


        sql = sql + sqlCondition + " limit ?,?";

        int beginIndex = (pageInfo.getPageIndex()-1)*pageInfo.getPageSize();

        List<SysUser> sysUserList = super.executeQuery(SysUser.class, sql, new Object[]{beginIndex, pageInfo.getPageSize()});
        pageInfo.setPageList(sysUserList);

        String sqlCount = "SELECT count(1) FROM  sys_user "+sqlCondition;
        Integer count = super.executeQuery(Integer.class, sqlCount, null).get(0);
        pageInfo.setCount(count);

        return pageInfo;
    }
}
